package com.itheima;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/*ServletConfig的演示*/
public class ServletConfigDemo extends HttpServlet {
   //1声明一个ServletConfig
    private ServletConfig config;
//通过init方法，来对ServletConfig进项赋值
    @Override
    public void init(ServletConfig config) throws ServletException {
        this.config=config;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//3演示ServletConfig常用方法
        //根据key获取value
        String encondingValue = config.getInitParameter("encoding");
        System.out.println(encondingValue);
        //获取所有的key
        Enumeration<String> keys = config.getInitParameterNames();
       while (keys.hasMoreElements()){
            //获取每一个key
           String key = keys.nextElement();
           String value = config.getInitParameter(key);
           System.out.println(key+","+value);
       }
       //获取Servlet的名称
        String servletName = config.getServletName();
        System.out.println(servletName);
        //获取ServletContext对象
        ServletContext servletContext = config.getServletContext();
        System.out.println(servletContext);
        //获取共享数据
        Object value = servletContext.getAttribute("username");
        System.out.println(value);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
